
package com.kugou.game.sdk.utils;

import com.kugou.framework.base.LogUtil;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.ui.activity.RechargeActivity;
import com.kugou.game.sdk.ui.activity.TopicActivity;

import org.json.JSONException;
import org.json.JSONObject;

import android.webkit.JavascriptInterface;

/**
 * 描述:本地方法管理类（提供给JS调用）
 * 
 * @author liux
 * @since 2014-10-30 上午10:22:06
 */
public class TopicMethodProvider {

    private static final String TAG = "TopicMethodProvider";

    private TopicActivity mActivity;

    public TopicMethodProvider(TopicActivity activity) {
        this.mActivity = activity;
    }

    private String getFailedJsonString() {
        JSONObject object = new JSONObject();
        try {
            object.put("status", 0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object.toString();
    }

    private String getSuccessJsonString() {
        JSONObject object = new JSONObject();
        try {
            object.put("status", 1);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return object.toString();
    }

    /**
     * @return 获取SDK信息
     */
    @JavascriptInterface
    public String getKugouSdkInfo() {
        LogUtil.d(TAG, "getKugouSdkInfo");
        JSONObject object = new JSONObject();
        try {
            object.put("status", 1);
            object.put("sdkVersionCode", SDKControler.getSDKVersionCode());
            object.put("sdkVersionName", SDKControler.getSDKVersionName());
            object.put("appid", SDKControler.getAppId());
            object.put("mid", AppUtil.getKugouMID(mActivity));
            object.put("imei", AppUtil.getIMEIByKG(mActivity));
            return object.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return getFailedJsonString();
        }
    }

    /**
     * @return 获取用户信息
     */
    @JavascriptInterface
    public String getKugouSdkUserInfo() {
        LogUtil.d(TAG, "getKugouSDkUserInfo");

        try {
            JSONObject object = new JSONObject();
            User user = UserManager.getInstance().getCurrentUserInternal();
            if (user == null) {
                return getFailedJsonString();
            }

            object.put("username", user.getAccountName());
            object.put("nickname", user.getNickName());
            object.put("openid", user.getOpenId());
            object.put("kgtoken", user.getKugouToken());
            object.put("kugouid", user.getKugouid());
            object.put("appid", SDKControler.getAppId());
            object.put("userType", user.isRegistered() ? 1 : 2);
            return object.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return getFailedJsonString();
        }
    }

    /**
     * 跳转到充值界面
     */
    @JavascriptInterface
    public String openKugouSdkRecharge() {
        LogUtil.d(TAG, "openKugouSdkRecharge");
        RechargeActivity.showYoubiRechargePage(mActivity, 4);
        return getSuccessJsonString();
    }

    /**
     * 关闭页面
     */
    @JavascriptInterface
    public String closeKugouSdkPage() {
        LogUtil.d(TAG, "closeKugouSdkPage");
        mActivity.finish();
        return getSuccessJsonString();
    }
}
